package com.ly.heart.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.ly.common.PageParam;
import com.ly.common.core.domain.AjaxResult;
import com.ly.common.core.domain.entity.SysDept;
import com.ly.common.core.domain.entity.SysUser;
import com.ly.common.utils.DateUtils;
import com.ly.common.utils.SecurityUtils;
import com.ly.common.utils.StringUtils;
import com.ly.common.utils.erp.SnowflakeIdWorker;
import com.ly.heart.domain.*;
import com.ly.heart.mapper.KingFinancePaySettlementBillMapper;
import com.ly.heart.service.*;
import com.ly.heart.util.CurrentUserUtil;
import com.ly.system.service.ISysDeptService;
import com.ly.system.service.ISysUserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 金蝶-财务-应付票据结算单Service业务层处理
 *
 * @author wutao
 * @date 2024-06-22
 */
@Service
public class KingFinancePaySettlementBillServiceImpl extends ServiceImpl<KingFinancePaySettlementBillMapper, KingFinancePaySettlementBillEntity> implements IKingFinancePaySettlementBillService {

    @Resource
    private KingFinancePaySettlementBillMapper kingFinancePaySettlementBillEntityMapper;
    @Resource
    private ICustomerService iCustomerService;
    @Resource
    private ISupplierService iSupplierService;
    @Resource
    private ISysDeptService iSysDeptService;
    @Resource
    private ISysUserService iSysUserService;
    @Resource
    private IKingBaseTransactionsCompanyService iKingBaseTransactionsCompanyService;
    @Resource
    private IKingBaseBankService iKingBaseBankService;

    /**
     * 查询金蝶-财务-应付票据结算单
     *
     * @param id 金蝶-财务-应付票据结算单ID
     * @return 金蝶-财务-应付票据结算单
     */
    @Override
    public KingFinancePaySettlementBillEntity selectKingFinancePaySettlementBillEntityById ( String id ) {
        KingFinancePaySettlementBillEntity paySettlementBillEntity = kingFinancePaySettlementBillEntityMapper.selectKingFinancePaySettlementBillEntityById ( id );
        String companyName = getCompanyName ( paySettlementBillEntity.getTransactionCompanyType (), paySettlementBillEntity.getTransactionCompanyId (), 1 );
        paySettlementBillEntity.setTransactionCompanyName ( companyName );
        return paySettlementBillEntity;
    }

    /**
     * 查询金蝶-财务-应付票据结算单列表
     *
     * @param kingFinancePaySettlementBillEntity 金蝶-财务-应付票据结算单
     * @return 金蝶-财务-应付票据结算单
     */
    @Override
    public AjaxResult selectKingFinancePaySettlementBillEntityList ( KingFinancePaySettlementBillEntity kingFinancePaySettlementBillEntity ) {
        kingFinancePaySettlementBillEntity.setPage ( ( kingFinancePaySettlementBillEntity.getPage () - 1 ) * kingFinancePaySettlementBillEntity.getLimit () );
        List<KingFinancePaySettlementBillEntity> kingFinancePaySettlementBillEntities = kingFinancePaySettlementBillEntityMapper.selectKingFinancePaySettlementBillEntityList ( kingFinancePaySettlementBillEntity );
        for ( KingFinancePaySettlementBillEntity paySettlementBillEntity : kingFinancePaySettlementBillEntities ) {
            String companyName = getCompanyName ( paySettlementBillEntity.getTransactionCompanyType (), paySettlementBillEntity.getTransactionCompanyId (), 2 );
            paySettlementBillEntity.setTransactionCompanyName ( companyName );
        }
        Integer count = kingFinancePaySettlementBillEntityMapper.selectKingFinancePaySettlementBillEntityCount ( kingFinancePaySettlementBillEntity );
        PageParam p = new PageParam ();
        p.setRecords ( kingFinancePaySettlementBillEntities ).setTotal ( count );
        return AjaxResult.success ( p );
    }

    /**
     * 新增金蝶-财务-应付票据结算单
     *
     * @param kingFinancePaySettlementBillEntity 金蝶-财务-应付票据结算单
     * @return 结果
     */
    @Override
    public int insertKingFinancePaySettlementBillEntity ( KingFinancePaySettlementBillEntity kingFinancePaySettlementBillEntity ) {
        kingFinancePaySettlementBillEntity.setId ( SnowflakeIdWorker.getInstance ().nextStringId () );
        kingFinancePaySettlementBillEntity.setDeptId ( CurrentUserUtil.getCompanyId () );
        kingFinancePaySettlementBillEntity.setCreateBy ( SecurityUtils.getLoginUser ().getUser ().getUserId () );
        kingFinancePaySettlementBillEntity.setCreateTime ( DateUtils.getNowDate () );
        return kingFinancePaySettlementBillEntityMapper.insertKingFinancePaySettlementBillEntity ( kingFinancePaySettlementBillEntity );
    }


    private String getCompanyName ( String companyType, String companyId, Integer type ) {
        if ( StringUtils.isBlank ( companyType ) || StringUtils.isBlank ( companyId ) ) {
            return "";
        }
        String transactionCompany = "";
        switch ( companyType ) {
            case "BD_Customer":
                CustomerEntity customerEntity = iCustomerService.getById ( companyId );
                if ( customerEntity != null && type == 1 )
                    transactionCompany = customerEntity.getCustomerName ();
                else if ( customerEntity != null && type == 2 )
                    transactionCompany = customerEntity.getCustomerCode ();
                break;
            case "BD_Supplier":
                SupplierEntity supplierEntity = iSupplierService.getById ( companyId );
                if ( supplierEntity != null && type == 1 )
                    transactionCompany = supplierEntity.getSupplierName ();
                else if ( supplierEntity != null && type == 2 )
                    transactionCompany = supplierEntity.getSupplierCode ();
                break;
            case "BD_Department":
                SysDept sysDept = iSysDeptService.getById ( companyId );
                if ( sysDept != null && type == 1 )
                    transactionCompany = sysDept.getDeptName ();
                else if ( sysDept != null && type == 2 )
                    transactionCompany = sysDept.getDeptCode ();
                break;
            case "BD_Empinfo":
                SysUser sysUser = iSysUserService.getById ( companyId );
                if ( sysUser != null && type == 1 )
                    transactionCompany = sysUser.getNickName ();
                else if ( sysUser != null && type == 2 )
                    transactionCompany = sysUser.getUserCode ();
                break;
            case "FIN_OTHERS":
                KingBaseTransactionsCompanyEntity transactionsCompanyEntity = iKingBaseTransactionsCompanyService.getById ( companyId );
                if ( transactionsCompanyEntity != null && type == 1 )
                    transactionCompany = transactionsCompanyEntity.getTitle ();
                else if ( transactionsCompanyEntity != null && type == 2 )
                    transactionCompany = transactionsCompanyEntity.getCompanyCode ();
                break;
            case "ORG_Organizations":
                SysDept organ = iSysDeptService.getById ( companyId );
                if ( organ != null && type == 1 )
                    transactionCompany = organ.getDeptName ();
                else if ( organ != null && type == 2 )
                    transactionCompany = organ.getDeptCode ();
                break;
            case "BD_BANK":
                KingBaseBankEntity bankEntity = iKingBaseBankService.getById ( companyId );
                if ( bankEntity != null && type == 1 )
                    transactionCompany = bankEntity.getTitle ();
                else if ( bankEntity != null && type == 2 )
                    transactionCompany = bankEntity.getCode ();
                break;
        }
        return transactionCompany;
    }
}
